package com.lagou.dao;

import com.lagou.io.Resources;
import com.lagou.pojo.User;
import com.lagou.sqlSession.SqlSession;
import com.lagou.sqlSession.SqlSessionFactory;
import com.lagou.sqlSession.SqlSessionFactoryBuilder;
import org.dom4j.DocumentException;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;

public class UserDaoImpl implements IUserDao{
    @Override
    public List<User> findAll() throws Exception {

        InputStream resourceAsStream= Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();


        List<User> users= sqlSession.selectList("user.selectList");
        for(User user1:users){
            System.out.println(user1);
        }
        return users;
    }

    @Override
    public User findByCondition(User user) throws Exception{

        InputStream resourceAsStream=Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //调用

        User user1= sqlSession.selectOne("user.selectOne", user);
        System.out.println(user1);
        return user1;
    }

    @Override
    public int updateUser(User user) throws Exception{
        InputStream resourceAsStream= Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        int a =sqlSession.update("user.updateUser",user);
        return a;
    }

    @Override
    public int insertUser(User user) throws Exception{
        InputStream resourceAsStream= Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        int a =sqlSession.update("user.insertUser",user);
        return a;
    }

    @Override
    public int deleteUser(User user) throws Exception {

        InputStream resourceAsStream= Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        int a =sqlSession.update("user.deletetUser",user);
        return a;
    }
}
